<?php
/**
 * PddMallLogisticsDao.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/12/11- 下午12:34
 */

namespace common\dao;


use common\constants\DefaultConst;
use common\exception\ValidatorException;
use common\models\PddMallLogistics;
use common\library\BaseDao;

class PddMallLogisticsDao extends BaseDao
{


    /**
     * 写入
     * @param array $data
     * @return PddMallLogistics
     * @throws ValidatorException
     */
    public function insertData(array $data)
    {
        $mallLogistics = new PddMallLogistics();

        $mallLogistics->load($data,'');

        if (!$mallLogistics->validate()) {
            throw new ValidatorException($mallLogistics->errorsToString());
        }

        if($mallLogistics->save())
        {
            return $mallLogistics;
        }else{
            throw new ValidatorException($mallLogistics->errorsToOneString());
        }
    }

    /**
     * 更新
     * @param PddMallLogistics $mallLogistics
     * @param array $data
     * @return PddMallLogistics
     * @throws ValidatorException
     */
    public function updateData(PddMallLogistics $mallLogistics, array $data)
    {
        $mallLogistics->load($data,'');

        if (!$mallLogistics->validate()) {
            throw new ValidatorException($mallLogistics->errorsToString());
        }

        if($mallLogistics->save())
        {
            return $mallLogistics;
        }else{

            throw new ValidatorException($mallLogistics->errorsToOneString());
        }
    }


    /**
     * 获取指定店铺的模板
     * @param int $uid
     * @param $mall_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getListByUidAndMallId(int $uid,$mall_id)
    {
        return PddMallLogistics::find()->select(['template_id','template_name','sf_free_type','cost_type'])->where([
            'uid' => $uid,
            'mall_id' => $mall_id,
            'status' => DefaultConst::IS_NORMAL
        ])->limit(500)->all();
    }



    /**
     * 通过template_id 获取模板信息
     * @param  $template_id
     * @return PddMallLogistics|null
     */
    public function getInfoByTmplateId($template_id)
    {
        return PddMallLogistics::findOne(['template_id'=>$template_id]);
    }
}